package org.xmap.net;

public final class URL {

    private String url; // url的格式protocol//hostname[:port]path
    private String urlWithoutFile; // 除去文件的url
    private String protocol; // 协议
    private String host; // 主机地址
    private String port; // 主机端口
    private String path; // 请求内容
    private String file; // 请求的文件

    public URL(String url) {
        this.reset(url);
    }

    /**
     * 获得指定字符串的url信息
     * @param url
     */
    public void reset(String url) {
        if (url == null) {
            return;
        }
        this.url = url;
        int beginIndex = 0;
        beginIndex = this.url.lastIndexOf('/');
        if (beginIndex > Protocol.PROTOCOL_HTTP.length() + 2 - 1) {
            this.urlWithoutFile = this.url.substring(0, beginIndex);
            this.file = this.url.substring(beginIndex + 1);
        } else {
            this.urlWithoutFile = this.url;
            this.file = "";
        }

        beginIndex = 0;
        int endIndex = this.url.indexOf(":");

        if (endIndex != -1) { // 获得协议
            endIndex++;
            this.protocol = this.url.substring(beginIndex, endIndex);
            endIndex += 2;
            beginIndex = endIndex;
        }
        endIndex = this.url.indexOf(":", beginIndex);
        this.host = "";
        this.port = "";
        if (endIndex != -1) { // 获得主机地址
            this.host = this.url.substring(beginIndex, endIndex);
            endIndex++;
            beginIndex = endIndex;
            endIndex = this.url.indexOf("/", beginIndex);
            if (endIndex != -1) {
                this.port = this.url.substring(beginIndex, endIndex);
                beginIndex = endIndex;
            } else {
                this.port = this.url.substring(beginIndex, this.url.length());
                beginIndex = this.url.length();
            }
        } else { // 可能是因为没有端口
            endIndex = this.url.indexOf("/", beginIndex);
            if (endIndex != -1) {
                this.host = this.url.substring(beginIndex, endIndex);
                beginIndex = endIndex;
                if (!this.protocol.equals(Protocol.PROTOCOL_FILE)) {
                    this.port = "80";
                }
            } else {
                this.host = this.url.substring(beginIndex);
                this.port = "80";
                beginIndex = this.url.length();
            }
        }

        this.path = this.url.substring(beginIndex);
        if (!this.path.startsWith("/")) {
            this.path = "/" + this.path;
        }
    }

    /**
     * 通过当前上下文获得新的链接地址
     * @param href
     * @return
     */
    public String fillUrl(String href) {
        if (href.indexOf(":") == -1) { // 相对地址
            if (href.startsWith("/")) { // 根目录
                href = this.protocol + "//" + this.host + ":" + this.port + href;
            } else { // 根目录
                href = this.urlWithoutFile + "/" + href;
            }
        }
        return href;
    }

    public String getUrl() {
        return this.url;
    }

    public String getUrlWithoutFile() {
        return this.urlWithoutFile;
    }

    public String getProtocol() {
        return this.protocol;
    }

    public String getHost() {
        return this.host;
    }

    public String getPort() {
        return this.port;
    }

    public String getPath() {
        return this.path;
    }

    public String getFile() {
        return this.file;
    }

    public static String appendParm(String url, String name, String value) {
        value = URLEncoder.encode(value, "UTF-8");
        if (url.indexOf('?') == -1) {
            url += ("?" + name + "=" + value);
        } else {
            url += ("&" + name + "=" + value);
        }
        return url;
    }
}